package com.ruoyi.common.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayFundTransCommonQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.ruoyi.project.system.domain.SmExtract;
import com.ruoyi.project.system.domain.SmPay;
import lombok.extern.slf4j.Slf4j;

import java.math.RoundingMode;

@Slf4j
public class AlibabaApi {

    public static AlipayFundTransToaccountTransferResponse transfer(SmExtract sysExtract) throws AlipayApiException {
//        if (1 == 1){
//            System.out.println("-------执行转账--------"+sysExtract);
//            return null;
//        }
        // MGZ TODO 2025/6/24: 执行转账
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        // 构造请求参数以调用接口
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
        // 设置商户转账唯一订单号
        model.setOutBizNo(sysExtract.getExtractId());
        // 设置收款方账户类型
        model.setPayeeType("ALIPAY_LOGONID");
        // 设置收款方账户
        model.setPayeeAccount(sysExtract.getAlipayAccount());
        // 设置收款方真实姓名（最长支持100个英文/50个汉字）
        model.setPayeeRealName(sysExtract.getAlipayName());
        // 设置转账金额
        model.setAmount(sysExtract.getAmount().toString());
        // 设置转账备注（支持200个英文/100个汉字）
        model.setRemark("天启数科转账");
        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
        log.warn(response.getBody());
        return response;
    }

    public static AlipayFundTransCommonQueryResponse query(SmExtract sysExtract) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        // 构造请求参数以调用接口
        AlipayFundTransCommonQueryRequest request = new AlipayFundTransCommonQueryRequest();
        AlipayFundTransCommonQueryModel model = new AlipayFundTransCommonQueryModel();
        // 设置销售产品码
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");
        // 设置商户转账唯一订单号
        model.setOutBizNo(sysExtract.getExtractId());
        request.setBizModel(model);
        return alipayClient.execute(request);
    }

    public static AlipayTradeRefundResponse refund(SmPay sysQueryPay) throws AlipayApiException {
//        if (1 == 1){
//            System.out.println("-------执行退款--------"+sysQueryPay);
//            return null;
//        }
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        // 设置商户订单号
        model.setOutTradeNo(sysQueryPay.getPlatformOrderNo());
        // 设置部分退款单号
        model.setOutRequestNo(sysQueryPay.getRemark());
        // 设置退款金额
        model.setRefundAmount(sysQueryPay.getPricing().setScale(2, RoundingMode.HALF_UP).toString());
        request.setBizModel(model);
        return alipayClient.execute(request);
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmEPkSOcMMBEYGHNf8OZ7OhJXYvQW44xGKl1eQI9hccncQH4JXSP+LAMBaHmetCd+kFkgY4nVOCVqrURSUYrgy5bCySLKiFH+jDT5SLlvp8eSvvO87xCE4wdNCjpX5MuNPqXkXnF8gOHPj2vkWDE6Drbyc84Ew3LNEG6UNcfn+i9OG3jFEsBRm4yKRMVR+c7tL0RVv3z6NGOB7FTXGuUssuYclWgj4r+MLCFkTFyiEtniNalHJfo4T7+zpY/WBtqCzCx7zpkHbMPiSLEGmJ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I9Vrq4wH3yHMjugCz7RvCwSz25ISep1FqAVEAUECgYEAtPTo/lKNQ7m84eAmALXqmE28e2guwEH+UAzahflsc7fyVublkqpfWQaJkVdH1TDPyOGK+rS0nFy/CjQaacqG2EyJziiD+0xveisbwkEnpaZMofGjYNbJtb5kGKc09G6BDZ1Fzsk5I9HtDId0cSzm2lug9mbEmkOB4hDHVUtdS+g=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhZzRHdRSdC3EAiTz8FQPxg9XWfJacxhUPEu9jmIwqEcJ43svhhEAWiWR7v+YngNmhgA0DVDYi2FIRowSxupbOdi+X7XNVYzmvyN0h8M0SlQNXg76udawGcWYekWB77Yd9hPZEG35dB/hsvJMHWyIl/gzZ272PkEjl4PCgNBr6Yrgsyr0S2Q/ifExl0HMUNO6t4qcpMNMuodc8nsLuM7CFPPACA1vNA5B8m6r7/Y+NRwCEp91ssi+X1XVZoh7KZ6yy6sAsxD8kVmPLuK0++ayu9Dmv9hhNHd0LpfJrLBfcpzaSWMRiuVhflLtkKT+ey6983I+XGKKDKnpU1mIoSloTwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021002113612356");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

}
